/*
 * author      : dormi330
 * date        : 2018/6/30
 * project     : mybatis
 * description :
 * 应用运行上下文
 * (1) 与领域无关的东西, 可以放到这里, 保持代码清晰移动
 * (2) 业务日志可以
 */

package com.deep.trade.application;

import com.deep.trade.common.CommonUtils;
import com.deep.trade.common.TradeException;
import org.springframework.stereotype.Service;

@Service
public class AppRuntimeEvn {

    // 租户编码
    private ThreadLocal<String> tenantCode = ThreadLocal.withInitial(() -> null);

    // token信息
    private ThreadLocal<String> token = ThreadLocal.withInitial(() -> null);

    public AppRuntimeEvn ensureToken(String token) throws Exception {
        if (null == token) throw new TradeException("token must not be null");
        this.token.set(token);
        return this;
    }

    public AppRuntimeEvn ensureTenantCode(String tenantCode) throws Exception {
        if (null == tenantCode) throw new TradeException("tenantCode must not be null");
        this.tenantCode.set(tenantCode);
        return this;
    }

    public String getTenantCode() {
        return tenantCode.get();
    }

    public String getToken() {
        return token.get();
    }

    // requestId
    private ThreadLocal<String> requestId = ThreadLocal.withInitial(CommonUtils::uuid);

    public String getRequestId() {
        return requestId.get();
    }
}
